
import * as puppeteer from 'puppeteer'
import { PuppeteerBase } from './PuppeteerBase';
import { LJPuppeteer } from './LJPuppeteer';
import * as util from '../util/util'
export class PuppeteerButton extends PuppeteerBase {

    //点击按钮
    async tapButton(ButtonSelector: string, ListenUrl?: string) {

        try {
            if (ListenUrl) {
                this.getAllRequest();
            }
            //获取元素
            await this.isdomsNotFound(ButtonSelector);
            let dom = await this.getdom(ButtonSelector);
            //点击
            await dom.tap();
        } catch (error) {
            throw error;
        }
    }

    //获取最新Modal选择器，用作判断最新模态框是否关闭
    async getLatestModalId(ModalSelector: string) {
        let AllModalId = await this.getAllModalIds(ModalSelector);
        AllModalId = util.sort_array(AllModalId, true);
        return `#layui-layer${AllModalId[0]}`;
    }

    //获取页面模态框ID末尾数字  判断模态框是否打开，只需要比对两次数组长度
    async getAllModalIds(ModalSelector: string) {

        return await LJPuppeteer.Page.$$eval(ModalSelector, e => {
            return e.map((modal) => {
                return modal.id.substr(11);
            })
        })
    }
}